<?php
/**
* @package controller
* @file indexController.php
* @date 15.09.2009
* @version 1.0
* @copyright sample
* @author Harinath Reddy
* @todo phpDoc comments
*/
require_once 'Zend/Controller/Action.php';

class journals_IndexController extends Cms_Controller_Action
{
	public function init()
	{
	  $this->view->siteTitle = 'Journals';
	  $this->currentPage = 1;
	  $this->resultsPerPage = 7;
	  $this->view->date_fromat = $this->CONFIG->date_format->date_time;
			
	}
	public function indexAction()
	{
	  	
			
	}
	public function favoritesAction()
	{
	
		$date = $this->_getParam('id') == ''? '':$this->_getParam('id');
		//echo $date;exit;
		
		//exit;
		if($date!='0')
		{
			$oldDate = trim($date);
			$arr = explode('-', $oldDate);
			$startDate = $arr[1].'-'.$arr[0].'-'.$arr[2];
			$nowdate=date('Y-m-d',strtotime($startDate));
			if($nowdate == date('Y-m-d'))
			{
				$nowdate=date('Y-m-d H:i:s');
			}
			else
			{
			$nowdate=date('Y-m-d',strtotime($startDate));
			}
		} else {
			$oldDate=date('m-d-Y');
			$arr = explode('-', $oldDate);
			$startDate = $arr[1].'-'.$arr[0].'-'.$arr[2];
			$nowdate=date('Y-m-d H:i:s',strtotime($startDate));
			if($nowdate == date('Y-m-d'))
			{
				$nowdate=date('Y-m-d H:i:s');
			}
			else
			{
			$nowdate=date('Y-m-d',strtotime($startDate));
			}
		}
		$arr = explode('-', $oldDate);
		$this->view->hilight = $arr[2].$arr[0].$arr[1];		
		//echo $startDate;exit;
		$this->view->params ="";
		if($_SESSION["sess_userid"]!="")
		{

			$memid=$_SESSION["sess_userid"];
			if($_POST)
			{
				$memid=$_SESSION["sess_userid"];
				//echo '<pre>';print_r($_POST);exit;
				$result = Doctrine::getTable('Journals')->addJournals($memid,$_POST,$startDate);
				if($result)
				{
					$content_type="journals";
					$share_type=1;
					$activity = Doctrine::getTable('EmActivities')->addActivity($result,$content_type,$memid,$share_type);
					$this->_redirect('/journals/index/journals/'.$oldDate);
				}
			}
			/* Colors */
			$myFavorites = Doctrine::getTable('MyFavorites')->getFavoritesByUserId($memid,$this->currentPage,$this->resultsPerPage,'',$startDate);
			//echo '<pre>';print_r($myFavorites);exit;
			$this->view->getFavorites=$myFavorites;
			$this->view->editDesc = '';
			$this->view->editId = 0;	
			$this->view->editcolorId = 0;	
			$this->view->startDate = $oldDate;

		}
		else
		{
			$this->_redirect("/home/");
		}
			
			
	}
		public function journalsAction()
	{
		
		$date = $this->_getParam('id') == ''? '':$this->_getParam('id');
		//echo $date;exit;
		
		//exit;
		if($date!='0')
		{
			$oldDate = trim($date);
			$arr = explode('-', $oldDate);
			$startDate = $arr[1].'-'.$arr[0].'-'.$arr[2];
			$nowdate=date('Y-m-d',strtotime($startDate));
			if($nowdate == date('Y-m-d'))
			{
				$nowdate=date('Y-m-d H:i:s');
			}
			else
			{
			$nowdate=date('Y-m-d',strtotime($startDate));
			}
		} else {
			$oldDate=date('m-d-Y');
			$arr = explode('-', $oldDate);
			$startDate = $arr[1].'-'.$arr[0].'-'.$arr[2];
			$nowdate=date('Y-m-d H:i:s',strtotime($startDate));
			if($nowdate == date('Y-m-d'))
			{
				$nowdate=date('Y-m-d H:i:s');
			}
			else
			{
			$nowdate=date('Y-m-d',strtotime($startDate));
			}
		}
		$arr = explode('-', $oldDate);
		$this->view->hilight = $arr[2].$arr[0].$arr[1];
		//echo $startDate;exit;
		$this->view->params ="";
		if($_SESSION["sess_userid"]!="")
		{

			$memid=$_SESSION["sess_userid"];
			if($_POST)
			{
				//echo $nowdate."and start date".$startDate;exit;
				//echo '<pre>';print_r($_POST);exit;
				$memid=$_SESSION["sess_userid"];
				//echo '<pre>';print_r($_POST);exit;
				$result = Doctrine::getTable('Journals')->addJournals($memid,$_POST,$nowdate);
				if($result)
				{
					$content_type="journals";
					$share_type=1;
					$activity = Doctrine::getTable('EmActivities')->addActivity($result,$content_type,$memid,$share_type);
					$this->_redirect('/journals/index/journals/'.$oldDate);
				}
			}
			/* Colors */
			$colors = Doctrine::getTable('MessageIntensity')->getColors();
			$this->view->colors=$colors;
				//echo $startDate;exit;
			$getJournals = Doctrine::getTable('Journals')->getJournalsByUserId($memid,$this->currentPage,$this->resultsPerPage,'',$startDate);
			//echo '<pre>';print_r($getJournals);exit;
			$this->view->getJournals=$getJournals;
			if(isset($_GET['edit']) || isset($_GET['saveas']))
			{
				if(isset($_GET['edit']))
				{
				
					$editedId = $_GET['edit'];
					$this->view->editId = $editDetails['id'];
				} else {
					$this->view->editId = 0;
					$editedId = $_GET['saveas'];
				}
				$editedId = base64_decode($editedId);
				$editDetails = Doctrine::getTable('Journals')->getDetails($editedId);
				$this->view->startDate = $oldDate;
				$this->view->editDesc = $editDetails['description'];
				$this->view->editcolorId = $editDetails['color_id'];

				
				
			}else {
				$this->view->editDesc = '';
				$this->view->editId = 0;	
				$this->view->editcolorId = 0;	
				$this->view->startDate = $oldDate;
			}

		}
		else
		{
			$this->_redirect("/home/");
		}
	}
	public function journalsViewAction()
	{
		$getId = $this->_getParam('id') == ''? 0:base64_decode($this->_getParam('id'));
		
		$oldDate = $this->_getParam('current_page') == '0'? '':$this->_getParam('current_page');
		$getDetails = Doctrine::getTable('Journals')->getDetails($getId);
		//echo '<pre>';print_r($getDetails);exit;
		$this->view->getDetails = $getDetails;	 
		$this->view->startDate = $oldDate;
		$this->view->siteTitle = $getDetails['description'];		
	}
	public function journalsDownloadAction()
	{
		$getId = $this->_getParam('id') == ''? 0:base64_decode($this->_getParam('id'));		
		$oldDate = $this->_getParam('current_page') == '0'? '':$this->_getParam('current_page');
		$getDetails = Doctrine::getTable('Journals')->getDetails($getId);		
		header("Content-type: application/vnd.ms-word");
		header("Content-Disposition: attachment; Filename=Journal.doc");
		$this->view->description = $getDetails['description'];
		$this->view->getDetails = $getDetails;	 
		$this->view->startDate = $oldDate;
		$this->view->siteTitle = $getDetails['description'];	
		exit;
	}
	public function journalsSettingsAction()
	{
	  $this->_helper->layout->disableLayout();	
			
	}
	public function paginationAction()
	{
		$memid=$_SESSION["sess_userid"];
		$this->view->params ="";
		$oldDate = $this->_getParam('id') == ''? '':$this->_getParam('id');
		if($oldDate!='')
		{
			$oldDate = trim($oldDate);
			$arr = explode('-', $oldDate);
			$startDate = $arr[1].'-'.$arr[0].'-'.$arr[2];
		} else {
			$startDate = '';
		}
		$currentPage = $this->_getParam('current_page') == 0? 1:$this->_getParam('current_page');
		$resultsPerPage = $this->_getParam('results_per_page') == 0? 12:$this->_getParam('results_per_page');
		$serValue = $this->_getParam('orderby') == ''? '':$this->_getParam('orderby');
		//echo $serValue;
		if($serValue == '0' || $serValue=='')
		{
			//echo 'ddddddddd';exit;
			$serValue = '';
		} else {
			$serValue = $serValue;
		}
		//echo $serValue;exit;
		$getJournals = Doctrine::getTable('Journals')->getJournalsByUserId($memid,$currentPage,$resultsPerPage,$serValue,$startDate);
		/* Colors */
		$colors = Doctrine::getTable('MessageIntensity')->getColors();
		$this->view->colors=$colors;
		$this->view->getJournals = $getJournals;
		$this->view->startDate = $oldDate;
		$jsonData['output'] = $this->view->render('index/journals-list.html');
		$jsonData['pages'] = $getJournals['pages'];
	    $jsonData['pager'] = $getJournals['pager'];
	    $jsonData['currentPage'] = $getJournals['currentPage'];
		$jsonData['resultsCount'] = $getJournals['resultsCount'];
		$this->_helper->json->sendJson($jsonData);
		//echo '<pre>';print_r($ideaDetails);exit;
		
	}
		public function favoritePaginationAction()
	{
		$memid=$_SESSION["sess_userid"];
		$this->view->params ="";
		$oldDate = $this->_getParam('id') == ''? '':$this->_getParam('id');
		if($oldDate!='')
		{
			$oldDate = trim($oldDate);
			$arr = explode('-', $oldDate);
			$startDate = $arr[1].'-'.$arr[0].'-'.$arr[2];
		} else {
			$startDate = '';
		}
		$currentPage = $this->_getParam('current_page') == 0? 1:$this->_getParam('current_page');
		$resultsPerPage = $this->_getParam('results_per_page') == 0? 12:$this->_getParam('results_per_page');
		$serValue = $this->_getParam('orderby') == ''? '':$this->_getParam('orderby');
		//echo $serValue;
		if($serValue == '0' || $serValue=='')
		{
			//echo 'ddddddddd';exit;
			$serValue = '';
		} else {
			$serValue = $serValue;
		}
		//echo $serValue;exit;
		$myFavorites = Doctrine::getTable('MyFavorites')->getFavoritesByUserId($memid,$currentPage,$resultsPerPage,$serValue,$startDate);
		/* Colors */
		$this->view->getFavorites = $myFavorites;
		$this->view->startDate = $oldDate;
		$jsonData['output'] = $this->view->render('index/favorites-list.html');
		$jsonData['pages'] = $myFavorites['pages'];
	    $jsonData['pager'] = $myFavorites['pager'];
	    $jsonData['currentPage'] = $myFavorites['currentPage'];
		$jsonData['resultsCount'] = $myFavorites['resultsCount'];
		$this->_helper->json->sendJson($jsonData);
		//echo '<pre>';print_r($ideaDetails);exit;
		
	}
	public function editAction()
	{
		$editedId = $_POST['id'];
		$startDate = $_POST['date'];
		$editDetails = Doctrine::getTable('Journals')->getDetails($editedId);
		$this->view->startDate = $startDate;
		$this->view->editDesc = $editDetails['description'];
		$this->view->editcolorId = $editDetails['color_id'];
		$this->view->editId = $editDetails['id'];
		$jsonData['output'] = $this->view->render('index/create-journals.html');
		$this->_helper->json->sendJson($jsonData);
	}
	
	public function printAction()
	{
		$this->_helper->layout->disableLayout();
		$getId = $this->_getParam('id') == ''? 0:base64_decode($this->_getParam('id'));
		$getDetails = Doctrine::getTable('Journals')->getDetails($getId);
		$this->view->getDetails = $getDetails;	 
	}
	public function deleteJournalAction()
	{
		$memid = $_SESSION["sess_userid"];
		$id = $_POST["id"];
		$current_page = $_POST["current_page"];
		$date = $_POST["date"];
		//echo '<pre>';print_r($_POST);exit;
		if($date!='0')
		{
			$oldDate = trim($date);
			$arr = explode('-', $oldDate);
			$startDate = $arr[2	].'-'.$arr[0].'-'.$arr[1];
			$nowdate=date('Y-m-d',strtotime($startDate));
			if($nowdate == date('Y-m-d'))
			{
				$nowdate=date('Y-m-d H:i:s');
			}
			else
			{
			$nowdate=date('Y-m-d',strtotime($startDate));
			}
		}
		//echo $startDate;exit;
		
			$result= Doctrine::getTable('Journals')->deleteJournalByJournalId($id);
			$getJournals = Doctrine::getTable('Journals')->getJournalsByUserId($memid,$current_page,$this->resultsPerPage,'',$startDate);
			//echo '<pre>';print_r($getJournals);exit;
			$this->view->getJournals=$getJournals;
			//echo '<pre>';print_r($getJournals);exit;
			$colors = Doctrine::getTable('MessageIntensity')->getColors();
			$this->view->colors=$colors;
			$this->view->params ='';
			$this->view->startDate = $date;
			$jsonData['output'] = $this->view->render('index/journals-list.html');
			$this->_helper->json->sendJson($jsonData);
	}
	public function downloadAction()
	{
		$description=$this->_getParam('id');
					$uniquerandomkey = mt_rand(10000, 99999);

		//Append unique random key to the word file name

		$wordfilename= 'convertedtoword'.$uniquerandomkey.'.doc';

		//Create temporary word file in the upload directory and then prepares this for writing.

		$fp = fopen("upload/$wordfilename", 'w+');

		//Assign the text content extracted from file_get_contents to $writetexttowordfile PHP variable

		$writetexttowordfile = $description;

		//Finally writes the text content to the newly created Word file

		fwrite($fp, $writetexttowordfile);

		//After writing the file, close it.

		fclose($fp);

		// Now that word file has been created, present download link to the client.

		$file = "upload/$wordfilename";
		echo '<h3>Download link</h3>';
		echo '<br />';
		echo '<a rel="nofollow" href="'.$file.'"><font size="2">Right click here then <b>Save as</b> to Download</a></font>';
		echo '<br />';
		echo '<br />';
 

	}

}	
	